/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [

    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'userType',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'in',
        hideQueryValue:[4,5,6],
        hide:true
    },
    {
        id: 'id',
        title: 'ID',
        type: 'number',
        columnClass: 'text-center width-100',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq'
    },
    {
        id: 'userName',
        title: '用户名',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'realName',
        title: '姓名',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'registerTime',
        title: '注册时间',
        type: 'date',
        format:'yyyy-MM-dd',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'mobilePhone',
        title: '手机号',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'userType',
        title: '类型',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: false,
        codeTable:{
            1:$lang.userAgentType.CUSTOMER,
            2:$lang.userAgentType.INVESTOR,
            3:$lang.userAgentType.SELLER,
            4:$lang.userAgentType.ONELEVEL,
            5:$lang.userAgentType.TWOLEVEL,
            6:$lang.userAgentType.TREELEVEL,
            7:$lang.userAgentType.AGENTSELLER
        }
    },
    {
        id: 'poundageScale',
        title: '综合管理费分佣比例',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: false
    },
    {
        id: 'deferScale',
        title: '递延费分佣比例',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: false
    },
    {
        id: 'profitScale',
        title: '利润分成比例',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: false
    }
    // ,
    // {
    //     id: 'operation',
    //     title: '操作',
    //     type: 'string',
    //     'export':false,
    //     columnClass: 'text-center',
    //     fastSort: false,
    //     resolution: function (value, record, column, grid, dataNo, columnNo) {
    //         var editHtml = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;编辑&nbsp;</a>";
    //         var html = "";
    //         // 判断是否有编辑权限
    //         if ($("#editAgent") && $("#editAgent").val() == 1 && record.userType == 4) {
    //             html += editHtml;
    //         }
    //         return html;
    //     }
    // }
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'agent/myList.json',
    exportFileName: '我的代理商',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int:["id","userType"],BigDecimal:["poundageScale","deferScale","profitScale"],Timestamp:["registerTime"]}
};

var grid = $.fn.DtGrid.init(dtGridOption);
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_del_flag'] = 1;
grid.fastQueryParameters['in_userType'] =[4,5,6];

//排序
grid.sortParameter.columnId = ['asc_id'];
// grid.sortParameter.sortType = 1;
//列表结束

var dtGridColumnsAgent = [
    {
        id: 'id',
        title: '编号',
        type: 'number',
        columnClass: 'text-center width-100',
        fastSort: true
    },
    {
        id: 'userType',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'userName',
        title: '用户名',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastQuery: true,
        fastQueryType: 'lk',
    },
    {
        id: 'mobilePhone',
        title: '手机号',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastQuery: true,
        fastQueryType: 'lk',
    },
    {
        id: 'realName',
        title: '姓名',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastQuery: true,
        fastQueryType: 'lk',
    }
];

//用户
var dtGridOptionAgent = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'agent/subordinat.json',
    exportFileName: '用户列表',
    columns: dtGridColumnsAgent,
    gridContainer: 'dtGridContainerAgent',
    toolbarContainer: 'dtGridToolBarContainerAgent',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int: ["id"], Timestamp: ["createTime"]},
    onRowDblClick: function (value, record, column, grid, dataNo, columnNo, cell, row, extraCell, e) {
        if (record.userName != null) {
            $("#chooseAgentAdd").text(record.userName);
        } else {
            $("#chooseAgentAdd").text("");
        }
        $("#addForm").find("input[name='id']").val(record.id);
        $("#agentModal").hide();//隐藏关联栏目窗口
        return false;
    }
};

var grid1 = $.fn.DtGrid.init(dtGridOptionAgent);

//排序

//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {

        //新增对话框初始化
        $("#addModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#addForm").psly().reset();
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            var datano = button.data('no');
            var gridData = grid.sortOriginalDatas[datano];
            var modal = $(this);
            modal.find("[name='id']").val("");
            modal.find("[name='poundageScale']").val("");
            modal.find("[name='profitScale']").val("");
            modal.find("[name='deferScale']").val("");
            $("#chooseAgentAdd").text("");
        });

        //选择用户列表
        $("#agentModal").on("show.bs.modal", function (event) {
            //获取接受事件的元素
            grid1.fastQueryParameters = new Object();
            grid1.fastQueryParameters['eq_del_flag'] = 1;
            grid1.fastQueryParameters['eq_user_type'] = 1;
            //排序
            grid1.sortParameter.columnId = 'id';
            grid1.sortParameter.sortType = 0;
            grid1.load();
        });

        //编辑对话框初始化
        // $("#editModal").on("show.bs.modal", function (event) {
        //     //清除错误信息
        //     $(".alert-danger").remove();
        //     $("#editForm").psly().reset();
        //     //获取接受事件的元素
        //     var button = $(event.relatedTarget);
        //     //获取data 参数
        //     var datano = button.data('no');
        //     var modal = $(this);
        //     //获取列表框中的原始数据
        //     var gridData = grid.sortOriginalDatas[datano];
        //     modal.find("[name='id']").val(gridData.id);
        //     modal.find("[name='userName']").val(gridData.userName);
        //     modal.find("[name='poundageScale']").val(gridData.poundageScale);
        //     modal.find("[name='profitScale']").val(gridData.profitScale);
        // });

        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['lk_user_name_or_like_real_name_or_like_mobile_phone'] = $('#keyword').val();
            grid.fastQueryParameters['eq_del_flag'] = 1;
            grid.fastQueryParameters['in_userType'] =[4,5,6];
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
    }

    //外部可调用
    return {
        bindEvent: _bindEvent
    }
}();
//操作处理结束

$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.bindEvent();
});